//
//  YCHomeItemCell.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/26.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCHomeItemCell.h"
#import <JSBadgeView/JSBadgeView.h>
#import "YCHomeCellModel.h"

#pragma mark -
#pragma mark -
#pragma mark - collectionViewCell

@interface YCItemCollectionCell : UICollectionViewCell

@property (weak ,nonatomic) UIImageView *iconImageView;
@property (weak ,nonatomic) UILabel *titleLabel;
@property (weak ,nonatomic) JSBadgeView *badgeView;
/** 数据模型  */
@property (nonatomic, strong) YCHomeCellModel *model;
@end

@implementation YCItemCollectionCell


- (instancetype)initWithFrame:(CGRect)frame
{
    if( self = [super initWithFrame:frame])
    {
        UIImageView *iconImageView = [[UIImageView alloc] init];
        iconImageView.contentMode = UIViewContentModeCenter;
        self.iconImageView = iconImageView;
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:iconImageView];
        
        // 添加badge视图
        JSBadgeView *badgeView = [[JSBadgeView alloc] initWithParentView:_iconImageView alignment:JSBadgeViewAlignmentTopRight];
        self.badgeView = badgeView;
        _badgeView.badgeBackgroundColor = [UIColor redColor];
        _badgeView.badgeTextFont = [UIFont systemFontOfSize:18];

        // 计算偏移距离
        _badgeView.badgePositionAdjustment = CGPointMake(-10,10);
        _badgeView.badgeText = @"1";// 等于nil,不会显示
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.backgroundColor = [UIColor whiteColor];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.text = @"张行";
        self.titleLabel = titleLabel;
        [self addSubview:self.titleLabel];
        
    }
    
    return self;
}

+ (BOOL)requiresConstraintBasedLayout
{
    return YES;
}

- (void)updateConstraints
{
    [super updateConstraints];
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(37);
        make.width.height.equalTo(@44);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView.mas_bottom).offset(8);
        make.centerX.equalTo(self.iconImageView);
        make.width.equalTo(@60);
        make.height.equalTo(@13);
    }];
}

- (void)setModel:(YCHomeCellModel *)model
{
    _model = model;
    if ([model.iconUrl isEqualToString:@"空"]) {
        self.iconImageView.image = [UIImage yc_imageWithColor:[UIColor whiteColor] size:CGSizeMake(44, 44)];
        self.titleLabel.text = @"";
    } else {
        self.iconImageView.image = [UIImage imageNamed:model.iconUrl];
        self.titleLabel.text = model.name;
    }
    self.badgeView.badgeText = model.badgeNumber==0?nil:[NSString stringWithFormat:@"%zd",model.badgeNumber];
}
@end

#pragma mark -
#pragma mark -
#pragma mark - tableViewCell

@interface YCHomeItemCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;


@end

@implementation YCHomeItemCell

- (void)setItems:(NSArray *)items
{
    _items = items;
    [self.collectionView reloadData];
}

- (NSInteger)updateWithModels:(NSArray *)models
{
    NSInteger count = models.count;
    NSInteger rows = (count - 1) / 3 + 1;
    CGFloat height = 85 * rows;//固定行高170/2
    return height;
}

// 快速创建一个tableViewCell
+ (instancetype)tableViewCellWithTableView:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"YCHomeItemCell";
    YCHomeItemCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[YCHomeItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
    }
    return cell;
}
// 初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.collectionView];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self.contentView);
        }];
    }
    return self;
}


- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        CGFloat width = kScreenWidth ;
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumInteritemSpacing = 0;//
        layout.minimumLineSpacing = 0.0;//
        layout.itemSize = CGSizeMake(width / 3.0,85); //
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:layout];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = [UIColor lightGrayColor];
        [_collectionView registerClass:[YCItemCollectionCell class] forCellWithReuseIdentifier:@"YCItemCollectionCell"];
    }
    
    return _collectionView;
}

#pragma mark -
#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.items.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YCHomeCellModel *model = self.items[indexPath.row];
    YCItemCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YCItemCollectionCell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}


#pragma mark -
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_cellDelegate respondsToSelector:@selector(homeItemCell:didSelectItemAtIndexPath:)]) {
        [_cellDelegate homeItemCell:collectionView didSelectItemAtIndexPath:indexPath];
    }
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


@end
